package cn.akira.domain.strategy.service.rule.chain.factory.engine.impl;

import cn.akira.domain.strategy.service.rule.chain.ILogicChain;
import cn.akira.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import cn.akira.domain.strategy.service.rule.chain.factory.engine.ILogicChainEngine;

import java.util.Map;

/**
 * @author fjl95
 * @date 2025/5/19
 * @description
 */
public class LogicChainEngine implements ILogicChainEngine {
    private final String[] ruleModels;
    private final Map<String, ILogicChain> logicChainGroup;

    public LogicChainEngine(Map<String, ILogicChain> logicChainGroup,String[] ruleModels) {
        this.ruleModels = ruleModels;
        this.logicChainGroup = logicChainGroup;
    }

    @Override
    public DefaultChainFactory.StrategyAwardVO process(String userId, Long strategyId) {
        ILogicChain firstChain;
        if (null == ruleModels || 0 == this.ruleModels.length){
            firstChain =  logicChainGroup.get("default");
        }else{
            //拼接链
            firstChain = logicChainGroup.get(ruleModels[0]);
            ILogicChain cur = firstChain;
            for (int i = 1; i < ruleModels.length; i++) {
                ILogicChain nextChain = logicChainGroup.get(ruleModels[i]);
                cur = cur.appendNext(nextChain);
            }
            cur.appendNext(logicChainGroup.get("default"));
        }
        DefaultChainFactory.StrategyAwardVO strategyAwardVO = firstChain.logic(userId, strategyId);
        return strategyAwardVO;
    }
}
